<template>
  <div>
    <el-row>
      <el-col :span="24">
        <p>缺省参数</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel" :list="formList" :cols="4" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="8">
        <div class="grid-content bg-purple-light">
          <p>预约</p>
          <FormPanel ref="formPanel1" :list="formList1" :cols="1" :labelWidth="140" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle1" />
        </div>
      </el-col>
      <el-col :span="8">
        <p>开工单</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel2" :list="formList2" :cols="1" :labelWidth="140" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle2" />
        </div>
      </el-col>
      <el-col :span="8">
        <p>结算</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel3" :list="formList3" :cols="1" :labelWidth="170" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle3" />
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <p>车间管理</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel4" :list="formList4" :cols="2" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle4" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <p>客户关怀</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel5" :list="formList5" :cols="2" :labelWidth="160" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle5" />
        </div>
      </el-col>
    </el-row>

    <div></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findAllRepair, updateBasicParam } from '@/api/repair/basicParameters';

export default {
  name: 'PartModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;
    this.saveData1 = null;
    this.saveData2 = null;
    this.saveData3 = null;
    this.saveData4 = null;
    this.saveData5 = null;
    return {
      formList: this.createFormList(),
      formList1: this.createFormList1(),
      formList2: this.createFormList2(),
      formList3: this.createFormList3(),
      formList4: this.createFormList4(),
      formList5: this.createFormList5()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  mounted() {
    this.queryDefaultPa();
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.partNo;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号前缀',
          fieldName: 'Repair_LicensePlate',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入车牌号前缀', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '缺省品牌',
          fieldName: 'basicom_brand',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省品牌', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省车系',
          fieldName: 'basicom_seriesCode',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省车系', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省车型',
          fieldName: 'basicom_carmodel',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省车型', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '三包年限(年)',
          fieldName: '1903',
          placeholder: '请输入...',
          readonly: true,
          rules: [{ required: false, message: '请输入三包年限(年)', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '电话区号',
          fieldName: 'basicom_phone',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入电话区号', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '缺省维修类型',
          fieldName: 'Repair_type',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省维修类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省工时单价根据',
          fieldName: '1291',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省工时单价根据', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '三包里程(公里)',
          fieldName: '1904',
          placeholder: '请输入...',
          readonly: true,
          rules: [{ required: false, message: '请输入三包里程(公里)', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '缺省仓库',
          fieldName: 'Repair_warehouse',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省仓库', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省维修项目类型',
          fieldName: '1082',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省维修项目类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '收费类别',
          fieldName: '1060',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择收费类别', trigger: 'change' }]
        }
      ];
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '提前进厂时间',
          fieldName: '1047',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>分钟前</div>;
          },
          rules: [{ required: false, message: '请输入提前进厂时间', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '延迟进厂',
          fieldName: 'Repair_delayMinuteInfactory',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>分钟后</div>;
          },
          rules: [{ required: false, message: '请输入延迟进厂', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '定期保养提醒后',
          fieldName: '1084',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天内回厂为提醒回厂</div>;
          },
          rules: [{ required: false, message: '请输入定期保养提醒后', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '客户流失报警提醒后',
          fieldName: '1094',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天内回厂为提醒回厂</div>;
          },
          rules: [{ required: false, message: '请输入客户流失报警提醒后', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '新车提醒后',
          fieldName: '1095',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天内回厂为提醒回厂</div>;
          },
          rules: [{ required: false, message: '请输入新车提醒后', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '保修到期提醒后',
          fieldName: '1225',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天内回厂为提醒回厂</div>;
          },
          rules: [{ required: false, message: '请输入保修到期提醒后', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '保险到期提醒后',
          fieldName: '1156',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天内回厂为提醒回厂</div>;
          },
          rules: [{ required: false, message: '请输入保险到期提醒后', trigger: 'blur' }]
        }
      ];
    },
    createFormList2() {
      return [
        {
          type: 'SELECT',
          label: '是否洗车',
          fieldName: 'Repair_carWash',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否洗车', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否路试',
          fieldName: '1068',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否路试', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否质检',
          fieldName: 'Repair_qualitycheck',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否质检', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否显示派工工时',
          fieldName: 'Repair_workingHours',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否显示派工工时', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '定期保养时间间隔',
          fieldName: '1038',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天</div>;
          },
          rules: [{ required: false, message: '请输入定期保养时间间隔', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '定期保养里程间隔',
          fieldName: '1072',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>公里</div>;
          },
          rules: [{ required: false, message: '请输入定期保养里程间隔', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '预交车预留',
          fieldName: '1063',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>分钟</div>;
          },
          rules: [{ required: false, message: '请输入预交车预留', trigger: 'blur' }]
        }
      ];
    },
    createFormList3() {
      return [
        {
          type: 'SELECT',
          label: '结算精度',
          fieldName: 'Repair_accuracy',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择结算精度', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '结算圆整方式',
          fieldName: 'Repair_mode',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择结算圆整方式', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省发票类型',
          fieldName: 'Repair_invoicetype',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省发票类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '缺省付款方式',
          fieldName: 'Repair_paymentMethod',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择缺省付款方式', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '结算应收款可调整幅度',
          fieldName: '1029',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入结算应收款可调整幅度', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '工单应收金额为0不自动结清',
          fieldName: '1096',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择工单应收金额为0不自动结清', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '提交结算时工单拆分',
          fieldName: '1098',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择提交结算时工单拆分', trigger: 'change' }]
        }
      ];
    },
    createFormList4() {
      return [
        {
          type: 'SELECT',
          label: '车间派工显示收费工时/工时费',
          fieldName: '1025',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择车间派工显示收费工时/工时费', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '车间派工显示备件单价/备件费',
          fieldName: '1051',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择车间派工显示备件单价/备件费', trigger: 'change' }]
        }
      ];
    },
    createFormList5() {
      return [
        {
          type: 'INPUT',
          label: '',
          fieldName: '1062',
          placeholder: '请输入...',
          unitRender: () => {
            return <div>天未进厂,自动标记为非激活客户</div>;
          },
          rules: [{ required: false, message: '请输入...', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '保修结束里程',
          fieldName: '1070',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入保修结束里程', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '保修结束天数',
          fieldName: '1071',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入保修结束天数', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '新车提醒预计提前天数',
          fieldName: '1078',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入新车提醒预计提前天数', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle1(val) {
      this.saveData1 = val;
    },
    formChangeHandle2(val) {
      this.saveData2 = val;
    },
    formChangeHandle3(val) {
      this.saveData3 = val;
    },
    formChangeHandle4(val) {
      this.saveData4 = val;
    },
    formChangeHandle5(val) {
      this.saveData5 = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      this.$refs.formPanel1.SUBMIT_FORM();
      this.$refs.formPanel2.SUBMIT_FORM();
      this.$refs.formPanel3.SUBMIT_FORM();
      this.$refs.formPanel4.SUBMIT_FORM();
      this.$refs.formPanel5.SUBMIT_FORM();
      let obj = Object.assign({}, this.saveData, this.saveData1, this.saveData2, this.saveData3, this.saveData4, this.saveData5);
      let res = await updateBasicParam(obj);
      if (res.resultCode === 200) {
        this.test();
      }
    },
    async queryDefaultPa() {
      let res = await findAllRepair();
      // 表1 缺省参数
      this.formList[0].initialValue = res.data.Repair_LicensePlate; // 车牌号前缀
      this.formList[1].initialValue = res.data.basicom_brand; // 缺省品牌
      this.formList[2].initialValue = res.data.basicom_seriesCode; // 缺省车系
      this.formList[3].initialValue = res.data.basicom_carmodel; // 缺省车型
      // 4  三包年限(年)
      this.formList[5].initialValue = res.data.basicom_phone; // 电话区号
      this.formList[6].initialValue = res.data.Repair_type; // 缺省维修类型
      // 7  缺省工时单价根据
      // 8  三包里程(公里)
      this.formList[9].initialValue = res.data.Repair_warehouse; // 缺省仓库
      // 10  缺省维修项目类型
      // 11  收费类别
      // 表2 预约
      // 0   提前进厂时间
      this.formList1[1].initialValue = res.data.Repair_delayMinuteInfactory; // 延迟进厂
      // 2   定期保养提醒后
      // 3   客户流失报警提醒后
      // 4   新车提醒后
      // 5   保修到期提醒后
      // 6   保险到期提醒后
      // 表3 开工单
      this.formList2[0].initialValue = res.data.Repair_carWash; // 是否洗车
      // 1   是否路试
      this.formList2[2].initialValue = res.data.Repair_qualitycheck; // 是否质检
      this.formList2[3].initialValue = res.data.Repair_workingHours; // 是否显示派工工时
      // 4   定期保养时间间隔
      // 5   定期保养里程间隔
      // 6   预交车预留
      // 表4  结算
      this.formList3[0].initialValue = res.data.Repair_accuracy; // 结算精度
      this.formList3[1].initialValue = res.data.Repair_mode; // 结算圆整方式
      this.formList3[2].initialValue = res.data.Repair_invoicetype; // 缺省发票类型
      this.formList3[3].initialValue = res.data.Repair_paymentMethod; // 缺省付款方式
      // 4   结算应收款可调整幅度
      // 5   工单应收金额为0不自动结清
      // 6   提交结算时工单拆分
      // 表5  车间管理

      // 表6  客户关怀
    },

    closeDialog(param) {
      this.$refs.formPanel.RESET_FORM();
      this.$refs.formPanel1.RESET_FORM();
      this.$refs.formPanel2.RESET_FORM();
      this.$refs.formPanel3.RESET_FORM();
      this.$refs.formPanel4.RESET_FORM();
      this.$refs.formPanel5.RESET_FORM();
      this.queryDefaultPa();
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
    }
  }
};
</script>

<style lang="less" scoped>
p {
  background-color: #d3dce6;
  height: 20px;
  line-height: 20px;
  margin-bottom: 10px;
}
.el-row {
  margin-bottom: 40px;
}
</style>
